//
//  AddContactViewController.m
//  17-Homework
//
//  Created by dllo on 16/8/6.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AddContactViewController.h"
#import "SQLiteDataBaseManager.h"
#import "Contact.h"
@interface AddContactViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *contactButton;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;
@property (weak, nonatomic) IBOutlet UITextField *contactInfoTextField;
@property (weak, nonatomic) IBOutlet UIButton *imageButton;
@property (nonatomic, strong) SQLiteDataBaseManager *dataBaseManager;
@end

@implementation AddContactViewController

- (void)viewDidLoad {
    self.dataBaseManager = [SQLiteDataBaseManager shareManager];
    [super viewDidLoad];
}

- (IBAction)clickContactButton:(id)sender {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *pick = [[UIImagePickerController alloc]init];
        pick.allowsEditing = YES;
        pick.delegate = self;
        pick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:pick animated:YES completion:^{
        }];
    }
}

- (IBAction)saveContact:(id)sender {
    Contact *contact = [[Contact alloc] init];
    contact.name = _nameTextField.text;
    contact.address = _addressTextField.text;
    contact.telNumber = [_contactInfoTextField.text integerValue];
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO);
    NSString *dataPath = [[pathArray firstObject] stringByAppendingPathComponent:@"imageData"];
    NSLog(@"%@", dataPath);
    NSData *data = UIImagePNGRepresentation(_imageButton.imageView.image);
    int result = [data writeToFile:dataPath atomically:YES];
    if (result) {
        NSLog(@"data写入成功");
        contact.imagePath = dataPath;
    } else {
        NSLog(@"data写入失败");
    }
    
    if (contact.name.length > 0 && self.delegate && [self.delegate respondsToSelector:@selector(addContact:)]) {
        [self.delegate addContact:contact];
        [_dataBaseManager openSQLite3];
        [_dataBaseManager insertContact:contact];
        [_dataBaseManager closeSQLite3];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)cancel:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(nonnull UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo{
    [_contactButton setImage:image forState:UIControlStateNormal];
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
}
#pragma mark - Memory Warning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
